<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------
// | Desc: 公众号开发默认继承基类
// +----------------------------------------------------------------------

namespace buwang\base;

use app\common\model\MemberMiniapp;
use think\facade\View;
use encrypter\Encrypter;

class Official extends BaseController{

    protected $bwapp;             //小程序miniapp信息
    protected $app_id;   //访问小程序ID
    protected $service_id;          //访问小程序的服务ID
    //protected $user;                //登录后用户
    protected $web_config;                 //站点参数

    /**
     * 初始化类
     */
    protected function initialize(){
        parent::initialize();
        //读取应用
        $this->bwapp  = self::apiAuth();
        if(!$this->bwapp){
            $this->error_jump('访问公众号服务出错');
        }
        //判断是否授权登录
        $this->user = app('userService')->getUser();//从session取用户数据(重写基类里面的$this->user)

        if(!$this->user){
            $this->redirect('api/official/auth',['app' => $this->bwapp->id,'url' => Encrypter::bwEncode(self::backUrl())]);
        }
        $ucode = app('userService')->setUcode();   //服务端缓冲邀请码并返回
        //设置常用参数
        $this->app_id     = $this->bwapp->id;
        $this->service_id            = $this->bwapp->service_id;
        $this->web_config            = bw_config('web_config'); //当前站点配置
        //$this->ucode                 = app('userService')->getUcode(); //获取邀请码
        //传参数到模板调用
        $assign['user']              = $this->user;
        $assign['invite_code']       = $this->user ? $this->user['invite_code']:'';
        $assign['web_config']        = $this->web_config;
        $assign['bwapp']           = $this->bwapp;
        $assign['app_id']          = $this->app_id;
        $assign['ucode']             = $ucode;
//var_dump($assign);die;
        // 模板全局变量赋值
        View::assign($assign);
    }

    /**
     * 判断当前页面的后退网址
     * @access protected
     * @return boolean
     */
    protected function backUrl(){
        $url     = input('backurl','','strip_tags');
        $referer = $this->request->header('referer');
        if (empty($url) && isset($referer)) {
            return $referer;
        }else{
            return empty($url) ? $this->request->url() : $url;
        }
    }

    /**
     * 接口认证 获取用户购买的应用的信息
     * @return void
     */
    protected function apiAuth() {
        $app = $this->request->param('service_id/s',0);
        if(!$app) $app= session('service_id');
        if(!$app) $this->error_jump('参数service_id错误');
        session('service_id', $app);//$app存在 设置存储下service_id
        return MemberMiniapp::where(['service_id' => $app,'is_lock' => 0])->field('id,appname,service_id,create_time,update_time,mp_appid,miniapp_appid')->cache(360)->find();
    }
}
